package sideral.ProcesamientoImagen;

import java.io.BufferedWriter;
import java.io.FileWriter;

import org.apache.log4j.Logger;

public class PLYFile{
	
	Linea l=new Linea();
	PixelGroup pg=new PixelGroup();
	static final Logger logger = Logger.getLogger(PLYFile.class);
	double x;
	double y;
	double z;
	
	public void writeFile(Imagen _image){
		try{
			FileWriter fstream = new FileWriter("pointCloud.ply");
			BufferedWriter out = new BufferedWriter(fstream);
			out.write("ply");
			out.newLine();
			out.append("format ascii 1.0");
			out.newLine();
			out.append("element vertex");
			out.append(" " + Captura.numTotalPixelesSeg);
			out.newLine();
			out.append("property float x");
			out.newLine();
			out.append("property float y");
			out.newLine();
			out.append("property float z");
			out.newLine();
			out.append("end_header");
			out.newLine();

			int contador=0;
			for(int i=0; i<_image.getSize(); i++)
			{
				l=_image.getLinea(i);
				for(int j=0; j<l.getSize(); j++)
				{
					pg=_image.getLinea(i).getPixelGroup(j);
					for(int k=0; k<pg.getSize(); k++)
					{
						x=pg.getPixel(k).x;
						y=pg.getPixel(k).y;
						z=pg.getPixel(k).z;
						
						out.append(x + " " + y + " " + z);
						out.newLine();
						++contador;
					}
				}
				
			}
			
			
			logger.info("Contador: " + contador);
			out.close();
		}catch(Exception e)
		{
			logger.error("Error: " + e.getMessage());
		}
	}
}